<?php
namespace Piwik\Plugins\AdsPlace\Lib;

use Piwik\Common;

/**
 * @package Piwik\Plugins\AdsPlace\Lib
 * @name  RedisAdapter
 * @todo  redis Reis对象,把 数据保存到 redis 的操作类；
 * 
 */
class RedisAdapter {
     
    protected  $redis;
    
    public function __construct() {
         
    }
    /**
     * 创建redis 链接服务器
     * @param string $ipstr =IP@PORT@PWD PWD可以省略；
     */
    public function connectReis( $ipstr = '127.0.0.1@6379')
    {
        $this->redis = new \Redis();
        if(empty($ipstr)){
            $ipstr = '127.0.0.1@6379';
        }
        list($ip, $port, $pw ) =   explode("@", $ipstr)  ;
        $this->redis->connect( $ip, $port);
        if(isset($pw) && !empty($pw)){
            $this->redis->auth( $pw );
        }      
    }
    public function ping(){
        return $this->redis->ping();
    }

    /**
     * 调用redis 的 set 方法
     * @param type $key
     * @param type $value
     */
    public function  set( $key, $value)
    {
        $this->redis->set($key, $value);
    }
    
    /**
     * 获取影音先锋 app广告 的字段信息
     * @return type
     */
    public function formatXfjson()
    {
        return [
           'as'=> Common::getRequestVar('as','0'),
           'as2'=> Common::getRequestVar('as2','0'),
           'as3'=> Common::getRequestVar('as3','0'),
           'opentxid'=> Common::getRequestVar('opentxid','') =='true'? '1': '0' ,
            'txid'=> Common::getRequestVar('txid',''),
            'opencsjid'=> Common::getRequestVar('opencsjid','') =='true'? '1':'0',
            'csjid'=> Common::getRequestVar('csjid',''),
            'dkxxlkp'=> Common::getRequestVar('dkxxlkp','') =='true'? '1':'0',
            'xxlkpid'=> Common::getRequestVar('xxlkpid',''),
           
            'dkcp'=> Common::getRequestVar('dkcp',''),
            'cpid'=> Common::getRequestVar('cpid','0'),
            'ver'=> Common::getRequestVar('ver',''),
            'vers'=> Common::getRequestVar('vers',''),
            'qv'=> Common::getRequestVar('qv',''),
            'qzgx'=> Common::getRequestVar('qzgx',''),
            'game'=> Common::getRequestVar('game','0'),
            'shortvdeo'=> Common::getRequestVar('shortvdeo','0'),       
          
       ];
    }
    /**
     * '{"vers": "2.2.0","ads": [
     * {"name": "tx","id": 9061105745045909,"open": 1,"order": 1,"bidecpm": 1000},
     * {"name": "cs","id": 887313752,"order": 1,"open": 1},
     * {"name": "bd","id": 7007014,"order": 0,"open": 1}
     * ]}
     */
    public function formatLybrowerJson()
    {
        return [
            'vers'=> Common::getRequestVar('vers',''),
            'ads' => [
                ['name'=> 'tx',
                    'id'=> Common::getRequestVar('txid',''),
                    'open'=> Common::getRequestVar('txopen','') =='true'? '1': '0' ,
                    'order'=>  Common::getRequestVar('txorder','0'),
                    'bidecpm'=> Common::getRequestVar('bidecpm','')],
                ['name'=> 'cs',
                    'id'=> Common::getRequestVar('csid',''),
                    'open'=> Common::getRequestVar('csopen','') =='true'? '1': '0' ,
                    'order'=>  Common::getRequestVar('csorder','0') ],
                ['name'=> 'bd',
                    'id'=> Common::getRequestVar('bdid',''),
                    'open'=> Common::getRequestVar('bdopen','') =='true'? '1': '0' ,
                    'order'=>  Common::getRequestVar('bdorder','0') ],
            ]
        ];
    }
}
